package org.gordonframework.site.forms.elements.skins {
	import org.gordonframework.core.graphics.GColor;
	import org.gordonframework.core.graphics.GDraw;
	import org.gordonframework.site.forms.elements.GElement;
	import org.gordonframework.site.forms.elements.GTextInput;
	import org.gordonframework.site.forms.elements.skins.GElementSkin;
	import org.gordonframework.site.views.GLabel;
	import org.gordonframework.site.views.GView;
	
	import flash.geom.ColorTransform;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;		

	/**
	 * @author eriarv
	 */
	public class GTextInputSkin extends GElementSkin {
		
		private var input : GLabel;
		private var help : GLabel;
		
		private var background : GView;
		private var border : GView;
		
		public function GTextInputSkin() {
			super();
			
			background = new GView();
			border = new GView();
			label = new GLabel();
			label.wordWrap = false;
			input = new GLabel();
			input.wordWrap = false;
			input.textField.type = TextFieldType.INPUT;
			input.textField.mouseEnabled = true;			input.textField.selectable = true;			input.textField.autoSize = TextFieldAutoSize.NONE;
			help = new GLabel();
			help.wordWrap = false;
			addChild( background );			addChild( border );			addChild( label );			addChild( input );			addChild( help );
			
		}

		override public function update(element : GElement) : void {
			
			super.update(element);
			
			background.graphics.beginFill(format.background.color, format.background.alpha);
			GDraw.rect(background.graphics, format.background.width, format.background.height);
			background.graphics.endFill();
			
			border.graphics.lineStyle( format.border.thickness, format.border.color, format.border.alpha );
			border.graphics.drawRect(0, 0, format.background.width, format.background.height);
			
			label.setDefaultTextFormat(textFormats.label);
			label.text = element.text;
			label.bottom = background.y;
			
			input.setDefaultTextFormat(textFormats.input);
			input.text = element.value;
			input.width = format.background.width - 2 * format.paddingX;			input.height = format.background.height - 2 * format.paddingY;
			input.x = format.paddingX;
			input.y = format.paddingY;
			
			help.setDefaultTextFormat(textFormats.help);
			help.text = (element as GTextInput).helpText;
			help.y = border.bottom;
			help.right = border.right;
			
			displayAsPassword = (element as GTextInput).displayAsPassword;
		}

		public function get value() : String {
			return input.text;
		}
		
		public override function set tabIndex( value : int ) : void {
			input.textField.tabIndex = value;
		}
		
		public function set displayAsPassword( value : Boolean ) : void {
			input.textField.displayAsPassword = value;
		}

		override public function set valid(value : Boolean) : void {
			var focusColor : uint = formatFactory.getSkinFormatForType(GElement.TYPE).focus.color;
			
			if ( value ) {
				label.transform.colorTransform = new ColorTransform();				border.transform.colorTransform = new ColorTransform();
			} else {
				label.transform.colorTransform = GColor.tint( focusColor );
				border.transform.colorTransform = GColor.tint( focusColor );
			}
		}
	}
}
